var ctxPath = "/education";

!function ($) {

    var initCollectionPage = function() {
        $.ajax({
            url: ctxPath + '/wx/api/customer/myCollection',
            type: 'post',
            dataType: 'json',
            success: function (result) {
                if(result.returnCode == 0) {
                    var collections = result.stpCollections;
                    if(collections.length==0){
                        $(".g-scrollview").append("<div class=\"shoppingCart\"><h2 class=\"text-blue text-center\"><a href=\"/education/wx/home.html\">收藏夹空空如也，去逛逛</a></h2></div>");
                    }
                    $(collections).each(function (index, ele) {
                        $(".g-scrollview").append("<div class=\"shoppingCart\"><article class=\"m-list list-theme4 pay-list collection-list\">" +
                            "<a href=\"/education/wx/studyToursProductDetail.html?id=" + ele.id + "\" class=\"list-item\"><div class=\"list-img\"><img src=\"" + ele.showImage + "\">" +
                            "</div><div class=\"list-mes\"><div class=\"list-info\"><h3 class=\"list-title\">" + ele.productName + "</h3>" +
                            "<p>" + ele.description + "</p></div><div class=\"list-mes-item\"><span class=\"list-price\"><em>¥</em>" + ele.deposit + "</span></a><span class=\"list-num del-list\" id =\"" + ele.id + "\" ptype=\"" + ele.productType +"\">删除</span>" +
                            "</div></div></article></div>");
                    })

                } else if(result.returnCode == -2){
                    alert(result.returnMsg);
                    window.location.href = "/education/wx/customerLogin.html?redirect=/education/wx/myCollects.html";
                } else {
                    alert(result.returnMsg);
                }
            }
        });
    };

    initCollectionPage();

    $("body").delegate(".list-num.del-list","click", function(){
        var productId = $(".list-num.del-list").attr('id');
        var productType = $(".list-num.del-list").attr('ptype');
        $.ajax({
            url: ctxPath + '/wx/api/customer/collect',
            type: 'post',
            dataType: 'json',
            data: {productId: productId, productType: productType},
            success: function (result) {
                window.location.href = ctxPath + "/wx/myCollects.html";
            }
        });
    });
}(jQuery);